import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import useStore from '@/store';

export const Layout = () => import('@/layout/index.vue');
/**
 * 使用export default时，对应的import语句不需要使用大括号；不使用export default时，对应的import语句需要使用大括号。
export default命令用于指定模块的默认输出。显然，一个模块只能有一个默认输出，因此export default命令只能使用一次。
所以，import命令后面才不用加大括号，因为只可能唯一对应export default命令
 */
import { localDevRoutes } from './modules/localDevRoutes'
//import localAsyncRoutes from './modules/localRoutes'
import localAsyncRoutes from './modules/localAsyncRoutes.json'
// 参数说明: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
// 静态路由
export const constantRoutes: Array<RouteRecordRaw> = [
	{
		path: '/redirect',
		component: Layout,
		meta: { hidden: true },
		children: [
			{
				path: '/redirect/:path(.*)',
				component: () => import('@/views/redirect/index.vue')
			}
		]
	},
	{
		path: '/login',
		component: () => import('@/views/login/index.vue'),
		meta: { hidden: true }
	},
	{
		path: '/404',
		component: () => import('@/views/error-page/404.vue'),
		meta: { hidden: true }
	},
	{
		path: '/401',
		component: () => import('@/views/error-page/401.vue'),
		meta: { hidden: true }
	},
	{
		path: '/',
		component: Layout,
		redirect: '/dashboard',
		children: [
			{
				path: 'dashboard',
				component: () => import('@/views/dashboard/index.vue'),
				name: 'Dashboard',
				meta: {
					title: '控制面板',
					icon: 'dashboard',
					affix: true,
				}
			},
		]
	}

	// 外部链接
	/*{
		  path: '/external-link',
		  component: Layout,
		  children: [
			  {
				  path: 'https://www.cnblogs.com/haoxianrui/',
				  meta: { title: '外部链接', icon: 'link' }
			  }
		  ]
	  }*/
	// 多级嵌套路由
	/* {
		   path: '/nested',
		   component: Layout,
		   redirect: '/nested/level1/level2',
		   name: 'Nested',
		   meta: {title: '多级菜单', icon: 'nested'},
		   children: [
			   {
				   path: 'level1',
				   component: () => import('@/views/nested/level1/index.vue'),
				   name: 'Level1',
				   meta: {title: '菜单一级'},
				   redirect: '/nested/level1/level2',
				   children: [
					   {
						   path: 'level2',
						   component: () => import('@/views/nested/level1/level2/index.vue'),
						   name: 'Level2',
						   meta: {title: '菜单二级'},
						   redirect: '/nested/level1/level2/level3',
						   children: [
							   {
								   path: 'level3-1',
								   component: () => import('@/views/nested/level1/level2/level3/index1.vue'),
								   name: 'Level3-1',
								   meta: {title: '菜单三级-1'}
							   },
							   {
								   path: 'level3-2',
								   component: () => import('@/views/nested/level1/level2/level3/index2.vue'),
								   name: 'Level3-2',
								   meta: {title: '菜单三级-2'}
							   }
						   ]
					   }
				   ]
			   },
		   ]
	   }*/
];

/**
 * 开发环境路由，比如用于代码生成器
 */
const env = import.meta.env.MODE

var _localDevRoutes = [] as any
if (env == "development") {
	_localDevRoutes = _localDevRoutes.concat(localDevRoutes)
}

/**
 * asyncRoutes本地动态路由，后期放在服务器
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutesOfLocal = _localDevRoutes.concat(localAsyncRoutes,
	[
		/** when your routing map is too long, you can split it into small modules **/
		// componentsRouter,
		// chartsRouter,
		// nestedRouter,
		// tableRouter,
		// 404 page must be placed at the end !!!
		{
			path: '*',
			redirect: '/404',
			hidden: true
		}
	]
);
// 创建路由
const router = createRouter({
	history: createWebHashHistory(),
	routes: constantRoutes as RouteRecordRaw[],
	// 刷新时，滚动条位置还原
	scrollBehavior: () => ({ left: 0, top: 0 })
});

// 重置路由
export function resetRouter() {
	const { permission } = useStore();
	permission.routes.forEach(route => {
		const name = route.name;
		if (name && router.hasRoute(name)) {
			router.removeRoute(name);
		}
	});
}

export default router;
